/*
 * @Author: 张宇飞
 * @Date: 2020-03-09 15:24:53
 * @LastEditors: 张宇飞
 * @LastEditTime: 2020-03-09 15:37:33
 * @Description:
 * @Email: zhangyufei49@163.com
 */
/*
 * @Author: 张宇飞
 * @Date: 2020-03-09 15:24:53
 * @LastEditors: 张宇飞
 * @LastEditTime: 2020-03-09 15:34:25
 * @Description: 栅栏的特性就是所有线程都达到统一的时间点后，才放行
 * @Email: zhangyufei49@163.com
 */
#define _GNU_SOURCE

#include <stdio.h>
#include <pthread.h>

static void* f(void* arg) {
    fprintf(stderr, "another thread start\n");
    sleep(3);
    fprintf(stderr, "another thread arrived barrier\n");
    pthread_barrier_wait((pthread_barrier_t*)arg);
    fprintf(stderr, "another thread passed barrier\n");
    return NULL;
}

int main(int argc, char const* argv[]) {
    fprintf(stderr, "main thread start\n");
    pthread_barrier_t barrier;
    pthread_barrier_init(&barrier, NULL, 2);

    pthread_t tid;
    pthread_create(&tid, NULL, f, &barrier);
    fprintf(stderr, "main thread arrived barrier\n");
    pthread_barrier_wait(&barrier);
    fprintf(stderr, "main thread passed barrier\n");
    pthread_barrier_destroy(&barrier);

    pthread_join(tid, NULL);

    return 0;
}
